package com.fishingwithme.android.ui.screens.my.points

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import com.fishingwithme.android.R
import com.fishingwithme.android.data.model.UserCredit
import com.fishingwithme.android.ui.screens.my.CreditHeaderSection
import com.fishingwithme.android.ui.theme.AppDimensions
import com.fishingwithme.android.ui.viewmodels.CreditUiState
import com.fishingwithme.android.ui.viewmodels.CreditViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreditScreen(
    viewModel: CreditViewModel = hiltViewModel()
) {

    val uiState by viewModel.uiState.collectAsState()

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(AppDimensions.SPACING_16)
    ) {
        when (val state = uiState) {
            is CreditUiState.Loading -> {
                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator()
                }
            }

            is CreditUiState.Error -> {
                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = state.message,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.error
                    )
                }
            }

            is CreditUiState.Success -> {
                    CreditHeaderSection(state.credit)
                    Spacer(modifier = Modifier.height(AppDimensions.SPACING_16))
                CreditProgressSection(state.credit)

            }
        }
    }
}

@Composable
private fun CreditProgressSection(creditData: UserCredit) {
    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_12)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppDimensions.SPACING_16)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = "${creditData.currentLevelName} (Lv.${creditData.currentLevel})",
                    style = MaterialTheme.typography.bodyMedium
                )

                Text(
                    text = "${creditData.currentLevelPoints}/${creditData.nextLevelPoints}",
                    style = MaterialTheme.typography.bodyMedium
                )
            }

            Spacer(modifier = Modifier.height(AppDimensions.SPACING_8))

            LinearProgressIndicator(
                progress = (creditData.progressPercentage),
                modifier = Modifier
                    .fillMaxWidth()
                    .height(AppDimensions.SPACING_8),
                color = MaterialTheme.colorScheme.primary,
                trackColor = MaterialTheme.colorScheme.surfaceVariant
            )

            Spacer(modifier = Modifier.height(AppDimensions.SPACING_8))

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = stringResource(
                        R.string.credit_need_more_points,
                        creditData.remainingPoints
                    ),
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.outline
                )

                Text(
                    text = "${creditData.nextLevelName} (Lv.${creditData.nextLevel})",
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.outline
                )
            }
        }
    }
}
